package com.nitty.cash.loop.common

import com.nitty.cash.loop.NittyApp
import com.nitty.cash.loop.base.BasePresenter
import com.nitty.cash.loop.request.IHttpCallback
import org.json.JSONObject

/**
 * CommonPresenter
 * @Author wwdeng
 * @CreateTime: 2024/11/20 10:32
 * @Annotation：
 */
abstract class CommonPresenter(private val iView: ICommonView): BasePresenter() {

    /**
     * 接口请求
     */
    fun doHttpPostAction(httpUrl: String, params: JSONObject, callback: IHttpCallback){
        iView.getAct().doPostAction(httpUrl, params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                       callback.onRequestSuccess(data)
                    }
                    else -> {
                        callback.onRequestError(data)
                        val msg = data.optString("msg")
                        iView.showNittyToast(msg)
                        iView.hiddenNittyLoading()
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                callback.onRequestError(error)
                iView.hiddenNittyLoading()
                iView.getAct().showRequestErrorDialog(error)
            }

            override fun onRequestFinish() {
                callback.onRequestFinish()
            }
        })
    }

    /**
     * 添加埋点
     */
    fun addEventPointRequestAction(eventName: String, eventRemark: String = "") {
        iView.getAct().addEventPointRequestAction(eventName,eventRemark)
    }

    /**
     * getString
     */
    fun getString(strId: Int): String{
        return NittyApp.app.getString(strId)
    }
}